﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
public partial class StoStatistic : System.Web.UI.Page
{
    public string totalnum = "";
    public string totalmon = "";
    public string Sqlstr = "";
    public string flag = "0";
    protected void Page_Load(object sender, EventArgs e)
    {    
        if (!IsPostBack)
        {
            if (Session["flag"] == null)//判断用户是否正常登陆
            {
                Response.Redirect("../../Login.aspx");
            }
            ddlStroe.Items.Add(new ListItem("-全部-"));
            ddlIN.Items.Add(new ListItem("-全部-"));
            ddlDepartment.Items.Add(new ListItem("-全部-"));
            ddlCustomer.Items.Add(new ListItem("-全部-"));
            
            this.Bind();
        }
    }
    public void Bind()
    {
        DataSet ds1 = DB.reDs("select Title from tb_BigType where Type='仓库'");
        ddlStroe.DataSource = ds1;
        ddlStroe.DataTextField = "Title";
        ddlStroe.DataBind();
        DataSet ds2 = DB.reDs("select Title from tb_BigType where Type='入库类型'");
        ddlIN.DataSource = ds2;
        ddlIN.DataTextField = "Title";
        ddlIN.DataBind();
        DataSet ds3 = DB.reDs("select Title from tb_BigType where type='部门'");
        ddlDepartment.DataSource = ds3;
        ddlDepartment.DataTextField = "Title";
        ddlDepartment.DataBind();
        DataSet ds4 = DB.reDs("select Name from tb_Enterprise where type1='是'");
        ddlCustomer.DataSource = ds4;
        ddlCustomer.DataTextField = "Name";
        ddlCustomer.DataBind();
    }
    protected void btnOK_Click(object sender, EventArgs e)
    {
        if (this.ddlStroe.SelectedValue != "-全部-")//判断用户是否以“存储仓库”为查询条件
        {
            if (flag == "0")//如果用户是以“存储仓库”为查询条件，再判断用户是否第一次输入条件
            {
                Sqlstr = Sqlstr + " where " + "B.StorePlace='" + this.ddlStroe.SelectedValue.ToString() + "'";//是第一次输入查询条件则在SQL查询语句中加入关键字“where”
            }
            else
            {
                Sqlstr = Sqlstr + "  and  " + "B.StorePlace='" + this.ddlStroe.SelectedValue.ToString() + "'";//不是第一次输入则在SQL查询语句中加入关键字“and”
            }
            flag = "1";
        }
        if (this.ddlIN.SelectedValue != "-全部-")//判断用户是否以“入库类型”为查询条件
        {
            if (flag == "0")
            {
                Sqlstr = Sqlstr + "  where  " + "B.StoreType='" + this.ddlIN.SelectedValue.ToString() + "'";
            }
            else
            {
                Sqlstr = Sqlstr + "  and " + "B.StoreType='" + this.ddlIN.SelectedValue.ToString() + "'";
            }
            flag = "1";
        }
        if (this.ddlDepartment.SelectedValue != "-全部-")//判断用户是否以“采购部门”为查询条件
        {
            if (flag == "0")
            {
                Sqlstr = Sqlstr + " where  " + "B.Department='" + this.ddlDepartment.SelectedValue.ToString() + "'";
            }
            else
            {
                Sqlstr = Sqlstr + " and " + "B.Department='" + this.ddlDepartment.SelectedValue.ToString() + "'";
            }
            flag = "1";
        }
        if (this.ddlCustomer.SelectedValue != "-全部-")//判断用户是否以“供货商”为查询条件
        {
            if (flag == "0")
            {
                Sqlstr = Sqlstr + " where " + "A.CustomerName='" + this.ddlCustomer.SelectedValue.ToString() + "'";
            }
            else
            {
                Sqlstr = Sqlstr + " and " + "A.CustomerName='" + this.ddlCustomer.SelectedValue.ToString() + "'";
            }
            flag = "1";
        }
        if (this.txtBuyMan.Text != "")//判断用户是否以“采购人”为查询条件
        {
            if (flag == "0")
            {
                Sqlstr = Sqlstr + " where " + "B.BuyMan='" + this.txtBuyMan.Text.Trim() + "'";
            }
            else
            {
                Sqlstr = Sqlstr + " and " + "B.BuyMan='" + this.txtBuyMan.Text.Trim() + "'";
            }
            flag = "1";
        }
        if (this.txtSign.Text != "")//判断用户是否以“签收人”为查询条件
        {
            if (flag == "0")
            {
                Sqlstr = Sqlstr + " where " + "B.Sign='" + this.txtSign.Text.ToString() + "'";
            }
            else
            {
                Sqlstr = Sqlstr + " and " + "B.Sign='" + this.txtSign.Text.ToString() + "'";
            }
            flag = "1";
        }
       //根据查询条件汇总物资数量及入库金额
        DataSet ds1 = DB.reDs("select sum(A.Amount) as amount , sum(A.Pay) as pay from tb_StoreDetails as A inner join tb_Store as B on A.StoreID=B.StoreID" + Sqlstr);
        //把汇总物资数量及入库金额分别赋值给全局变量totalnum和totalmon
        totalnum = ds1.Tables[0].Rows[0][0].ToString();
        totalmon = ds1.Tables[0].Rows[0][1].ToString();
       //根据符合条件的记录绑定到DataList控件上
        DataSet ds = DB.reDs("select distinct  A.WzID, A.Name,A.Model,A.Spec,A.Card from tb_StoreDetails as A inner join tb_Store as B on A.StoreID=B.StoreID" + Sqlstr);
        this.DataList1.DataSource = ds;
        this.DataList1.DataBind();     
    }
}
